package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.customview.DateViewDialog;
import com.cloudsigns.factorymanager2.project.adapter.EngineeringInformationAdapter;
import com.cloudsigns.factorymanager2.project.iview.IAddEngineeringBatchInstallationOrderView;
import com.cloudsigns.factorymanager2.project.presenter.AddEngineeringBatchInstallationOrderPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * 批量安装单
 * Created by tony on 2017/12/11.
 */

public class AddBatchInstallationOrderActivity extends Activity implements View.OnClickListener, IAddEngineeringBatchInstallationOrderView {
    private View rl_back;
    private TextView tv_title;
    private View img_edit_engineering_information;
    private ListView lv;
    private Button btn_save;
    private EditText edt_completion_date, edt__project_name;
    private ImageView iv_completion_date;
    private EngineeringInformationAdapter adapter;
    private List<EngineeringInstallationOrderBean> engineeringBeanList = new ArrayList<>();
    private final String TAG = "AddBatchInstallationOrderActivity";
    private AddEngineeringBatchInstallationOrderPresenter addEngineeringBatchInstallationOrderPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_batch_installation_order);

        addEngineeringBatchInstallationOrderPresenter = new AddEngineeringBatchInstallationOrderPresenter(this);
        init();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        img_edit_engineering_information = findViewById(R.id.img_edit_engineering_information);
        lv = findViewById(R.id.lv);
        btn_save = findViewById(R.id.btn_save);
        edt__project_name = findViewById(R.id.edt__project_name);
        edt_completion_date = findViewById(R.id.edt_completion_date);
        iv_completion_date=findViewById(R.id.iv_completion_date);

        tv_title.setText(getResources().getText(R.string.batch_installation_order));
        rl_back.setOnClickListener(this);
        btn_save.setOnClickListener(this);
        img_edit_engineering_information.setOnClickListener(this);
        iv_completion_date.setOnClickListener(this);

        adapter = new EngineeringInformationAdapter(this, TAG);
        adapter.setData(engineeringBeanList);
        lv.setAdapter(adapter);
    }

    private void save() {
        addEngineeringBatchInstallationOrderPresenter.addEngineeringBatchInatallationOrder();
//        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
//        finish();
    }

    private void showDateDialog() {
        DateViewDialog endDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt_completion_date.setText(time);
                    }
                });
        endDateChooseDialog.setTimePickerGone(true);

        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());

        endDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        endDateChooseDialog.showDateChooseDialog();

    }

    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
                // 工程信息
            case R.id.img_edit_engineering_information:
                mIntent = new Intent(AddBatchInstallationOrderActivity.this, AddEngineeringInfomartionActivity.class);
                startActivityForResult(mIntent, ConstantUtil.ADD_PRODUCT_LIST);
                break;
            case R.id.btn_save:
                save();
                break;
            case R.id.iv_completion_date:
                showDateDialog();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.ADD_PRODUCT_LIST: //添加产品清单
                if (data != null) {
                    EngineeringInstallationOrderBean engineeringBean = (EngineeringInstallationOrderBean) data.getSerializableExtra(ConstantUtil.ENGINEERING_INFO);

                    engineeringBeanList.add(engineeringBean);
                    adapter.setData(engineeringBeanList);
                    adapter.notifyDataSetChanged();
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public long getCompleteDate() {
        long mills = DateUtil.getMillis(edt_completion_date.getText().toString().trim());
        return mills;
    }

    @Override
    public List<EngineeringInstallationOrderBean> getEngineeringBeanList() {
        return engineeringBeanList;
    }

    @Override
    public String getProjectName() {
        return edt__project_name.getText().toString().trim();
    }

    @Override
    public void addEngineeringBatchInstallatonOrderSuccess(String projectId) {
        Toast.makeText(this, "保存成功" + projectId, Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, true);
        setResult(ConstantUtil.CREATED_ORDER, mIntent);
        finish();
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, "保存失败" + desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
